//右上角页面切换
$('.right-titleicon').on("click",function(e){
	projectVM.showMenulist()
})

 var cloak = ''
		projectVM = new Vue({ 
			el: "#content",
			components:{ 
				personselect
			},
			data: {
				dutyInfo:'',
				userInfo:'',
				keyWords:'',
				page:1,
				keyStatus:3,
				isSingle:true,
				onStatus:false,
				showPop:false,
				showMenu:false,
				keyWords:'',
				menuList:[
					{name:'借用',path:'borrow.html'},{name:'归还',path:'return.html'},{name:'列表',path:'index.html'},{name:'我值班'},
				],
				filterText:'all', //筛选条件
				list:[]
			},
			mounted: function() {
				console.log(JSON.stringify(localStorage.getItem("neiosUser")))
				this.userInfo = JSON.parse(localStorage.getItem("neiosUser"))
				this.getList()
				this.getDuty()
			},
			methods: {
				//清空搜索框
				clearInput(){
					this.keyWords = ''
					this.page = 1
					this.list = []
					this.getList()
				},
				//设置值班人
				setDuty(){
					var that =  this
					var url = '/app/currentEntry/keyOperate/saveDutyStaff'
					var that = this
					let param = {
						currentUser:this.userInfo,
						dutyStaffCode:this.userInfo.userId,
						dutyStaffName:this.userInfo.name
					}
					mui.neiosRequst.httpRequst(url, param,
						function(data) {
							that.onStatus = true
						},
						function() {
							console.log("查询失败!");
					})
				},
				//获取值班人
				getDuty(){
					var url = '/app/currentEntry/keyOperate/getDutyStaff'
					var that = this
					let param = {
						currentUser:this.userInfo
					}
					mui.neiosRequst.httpRequst(url, param,
						function(data) {
							if(data.data == null){
								
							}
							else{
								that.dutyInfo = data.data
								if(data.data.dutyStaffCode == that.userInfo.userId){
									that.onStatus = true
								}
							}
						},
						function() {
							console.log("查询失败!");
					}
			);
				},
				//清空搜索框
				clearInput(){
					this.keyWords = ''
					this.page = 1
					this.list = []
					this.getList()
				},
				//编辑
				goEdit(e){
					mui.openWindow({
						url: 'borrow.html',
						id:  'borrow.html',
						extras: {
							keyOperate:JSON.stringify(e)
						},
					waiting: {
					autoShow: false
					}
					});
					localStorage.setItem('keyOperate',JSON.stringify(e))
				},
				onChange(e){
					clearTimeout(cloak)
				this.page = 1
				this.list = []
				cloak = setTimeout(() => {
					this.getList()
				}, 500);
				},
			goDetail(e){
				
				if(this.keyStatus != 1){
					mui.openWindow({
						url: 'detail.html',
						id:  'detail.html',
						extras: {
							keyOperate:JSON.stringify(e)
						},
					waiting: {
					autoShow: false
					}
					});
				}
				else{
					mui.openWindow({
						url: 'returnDetail.html',
						id:  'returnDetail.html',
						extras: {
							keyOperate:JSON.stringify(e)
						},
					waiting: {
					autoShow: false
					}
					});
				}
				console.log("=====钥匙详情传递参数=====",JSON.stringify(e))
				localStorage.setItem('keyOperate',JSON.stringify(e))
			},
			//获取列表
		    getList(){
				console.log("keyStatus============",this.keyStatus)
				if(this.keyStatus == 1){
					var url = '/app/currentEntry/keyOperate/getKeyReturnList'
				}
				else if(this.keyStatus == 2){
					var url = '/app/currentEntry/keyOperate/getKeyBorrowList'
				}else if(this.keyStatus == 0){
					var url = '/app/currentEntry/keyOperate/getToRepayKeyList'
				}else{
					var url = '/app/currentEntry/keyOperate/getKeyBorrowList'
				}
				var that = this
				let param = {
					keyStatus:this.keyStatus,
					pageIndex:this.page,
					pageSize:10,
					areaCompanyCode:this.userInfo.companyNewCode,
					stationCode:this.userInfo.stationNewCode,
					role:this.userInfo.roles,
					searchType:0,
					keyWord:this.keyWords
				}
				if(this.keyStatus == 0){
					 param = {
						keyCode:this.keyCode,
						pageIndex:this.page,
						pageSize:10,
						newStationCode:this.userInfo.stationNewCode,
					}
				}
				mui.neiosRequst.httpRequst(url, param,
					function(data) {
						mui('#scroll').pullRefresh().endPulldownToRefresh(true);
						console.log("URL=======",url)
						console.log("param=======",JSON.stringify(param));
					    console.log("data"+JSON.stringify(data))	
						if(data.code == '0'){
							if(that.page == 1){
								that.list = data.data.records
							}
							else{
								that.list = that.list.concat(data.data.records)
							}
												
							if(that.page < data.data.pages){
								that.page += 1
								mui('#scroll').pullRefresh().endPullupToRefresh();
							}
							else{
								if(that.page == 1 && data.data.records.lentgh  ==0){
									mui.toast('暂无数据')
								}
							
								mui('#scroll').pullRefresh().endPullupToRefresh(true);
								
												
							}
						}else{
							mui.toast('查询失败')
						}					
					},
					function() {
						console.log("查询失败!");
						 mui('#scroll').pullRefresh().endPullupToRefresh(true);
						 mui('#scroll').pullRefresh().endPulldownToRefresh(true);
					}
				);
			},
			//阻止冒泡
				clickStop(){

				},
				
				navTo(e){
					mui.openWindow({
            			url: e,
            			id: e,
						extras: {},
					waiting: {
					autoShow: false
					}
        			});
				},
				//点击菜单
				menuClick(e){
					var that = this
					if(e.name == '我值班'){
						if(this.dutyInfo.dutyStaffCode == this.userInfo.userId){
							mui.toast('你已经是值班人')
							return
						}
						var btnArray = ['否', '是'];
						mui.confirm('确定设置自己为值班人吗', '提示', btnArray, function(e) {
							if(e.index == 1) {
								that.setDuty()		
							} else {}
						})						
					}
					else{
						this.showMenu = false
						this.navTo(e.path)
					}
				},
				//左上角菜单显示隐藏
				showMenulist(){
					this.showMenu?this.showMenu=false:this.showMenu=true
				},
				//切换筛选条件
				changeFliter(e){
					this.filterText = e
					this.page = 1
					if(e == 'all'){
						this.keyStatus= 3
					}
					else if(e == 'unreturn'){
						this.keyStatus= 0
					}
					else if(e == 'return'){
						this.keyStatus= 1
					}
					this.page = 1
					this.list = []
					this.getList()
				},
				showPop(){
					this.show_popup = true
				},
				hidePopup(){
				this.show_popup = false	
				},
				
			}
			
			})

		

			mui.init({
				pullRefresh: {
				  container: "#scroll", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
				  down: { //下拉刷新
					style: 'circle',
					height: 50, //可选,默认50.触发下拉刷新拖动距离,
					auto: false, //可选,默认false.首次加载自动下拉刷新一次
					contentdown: "下拉可以刷新", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
					contentover: "释放立即刷新", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
					contentrefresh: "正在刷新...", //可选，正在刷新状态时，下拉刷新控件上显示的标题内容
					callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
						mui('#scroll').pullRefresh().refresh(true);
						
						projectVM.page = 1
						
						projectVM.getList()
						
					  ////没有更多内容了，endPulldown 传入true， 不再执行下拉刷新
				
					   }
				  },
				  up: { //上拉加载
					height: 50, //可选.默认50.触发上拉加载拖动距离
					auto: false, //可选,默认false.自动上拉加载一次
					contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
					contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
					callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
						
						projectVM.getList()
						
					}
				  }
				}
			  }); 

			  mui('.mui-input-clear')[0].addEventListener('focus',function(){
				mui('.mui-icon-clear')[0].addEventListener('tap',function(){
					projectVM.clearInput()
				})
			})		  
	window.addEventListener('refresh', function(e){//执行刷新
	location.reload();
	});